package com.lalifa.googledate.ui.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.RecyclerView
import com.lalifa.extension.onClick
import com.lalifa.extension.text
import com.lalifa.extension.toast
import com.lalifa.googledate.R
import com.lalifa.googledate.api.CountryBean
import com.lalifa.googledate.api.CountryListBean
import com.lalifa.googledate.api.HolidayUp
import com.lalifa.googledate.ui.adapter.countryAdapter
import com.lalifa.googledate.util.Tool
import com.lalifa.utils.GsonUtil
import com.lalifa.utils.LiveDataBus
import com.lalifa.utils.SPUtil
import com.lalifa.utils.UIKit


/**
 * 礼物弹框
 */
class CountyDialog(
    val act: AppCompatActivity, val get: String
) : DialogFragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.MyDialog)
    }

    override fun onStart() {
        super.onStart()
        initWindow()
    }

    /**
     * 获取手机屏幕宽度
     */
    private fun getW(): Int {
        return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
            act.windowManager.currentWindowMetrics.bounds.width()
        } else {
            val dm = DisplayMetrics()
            act.windowManager.defaultDisplay.getRealMetrics(dm)
            dm.widthPixels
        }

    }

    /**
     * 获取手机屏幕宽度
     */
    private fun getH(): Int {
        return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
            act.windowManager.currentWindowMetrics.bounds.height()
        } else {
            val dm = DisplayMetrics()
            act.windowManager.defaultDisplay.getRealMetrics(dm)
            dm.heightPixels
        }

    }

    private fun initWindow() {
        //初始化window相关表现
        val window = dialog?.window
        //设备背景为透明（默认白色）
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        //window?.attributes?.width = matchp
        //window?.attributes?.height = 350
        val attributes = window!!.attributes
        attributes.gravity = Gravity.BOTTOM //下方
        attributes.width = getW() //满屏
        attributes.height = getH() - 400
        window.attributes = attributes
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //加载布局
        val view = inflater.inflate(R.layout.pop_country, container)
        initView(view)
        return view
    }

    //初始化View
    private fun initView(view: View) {
        val closePop = view.findViewById<AppCompatImageView>(R.id.closePop)
        val etName = view.findViewById<AppCompatEditText>(R.id.etName)
        val search = view.findViewById<TextView>(R.id.search)
        val list = view.findViewById<RecyclerView>(R.id.listCountry)
        val adapter = list.countryAdapter().apply {
            R.id.item_county.onClick {
                act.showCountrySetDialog(getModel()) {
                    if (it != getModel<CountryBean>().isPublic) {
                        getModel<CountryBean>().isPublic = it
                        notifyDataSetChanged()
                        UIKit.postDelayed({
                            val counterList = CountryListBean(models as List<CountryBean>)
                            val obj2Json = GsonUtil.obj2Json(counterList)
                            SPUtil.setSp(Tool.holidayName, obj2Json)
                            LiveDataBus.get().with("holidayList").value = HolidayUp(1)
                        }, 500)
                    }
                }
            }
        }
        closePop.onClick { dismiss() }
        val codeList = GsonUtil.json2Obj(get, CountryListBean::class.java)
        adapter.models = codeList.countryList
        search.onClick {
            val text = etName.text()
            if (text.isEmpty()) {
                act.toast(getString(R.string.place_input_event_title))
                return@onClick
            }
            val filter = codeList.countryList.filter { item -> item.name.contains(text) }
            adapter.models = filter
        }
    }
}